home *** CD-ROM | disk | FTP | other *** search
- Path: pop.gnn.com!HoangTQ
- From: Tuyen Hoang <HoangTQ@gnn.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Need Help With CSC Homework
- Date: Sat, 16 Mar 1996 17:25:23
- Organization: GNN
- Message-ID: <4iff26$p2i@news-e2b.gnn.com>
- References: <4g0qun$jp@news.nevada.edu>
- NNTP-Posting-Host: www-41-87.gnn.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset="us-ascii"
- X-GNN-NewsServer-Posting-Date: 16 Mar 1996 22:24:06 GMT
- X-Mailer: GNNmessenger 1.2
-
-
-
- pfun.cpp
-
- //*************************************************
- //** ROMANFUN.CPP **
- //*************************************************
- //** This program will add, subtract, multiply, **
- //** and divide roman numbers. The numbers are **
- //** assumed to be less than 20 characters long. **
- //** The roman numbers should not be inputed in **
- //** subtraction form. Examples - IV=6 IIII=4 **
- //*************************************************
- #include <iostream.h> //for cin and cout
- #include "p.h" //has typedef and prototypes
- istream& operator>>(istream& in,Romantype& x)
- {
- in>>x.Roman;
- return in;
- }
-
- ostream& operator<<(ostream& out,Romantype x)
- {
- out<<x.Roman;
- return out;
- }
-
- Romantype::Romantype()
- {
- int i;
- for(i=1;i<MAXLENGTH;i++)
- {
- Roman[i]=' ';
- }
-
- //and Roman[MAXLENGTH]???
- }
-
- //*************************************************
- //** ADD FUNCTION **
- //*************************************************
- //** This function adds two roman numbers. It **
- //** requires a character array and two roman **
- //** numbers. **
- //*************************************************
- void Romantype::operator+(Romantype numbertwo)
- {
- int numbero;
- int numbert;
- int answer;
- Romantype answerr;
- numbero=RomanToDecimal(Roman);
- numbert=RomanToDecimal(numbertwo.Roman);
- answer=numbero+numbert;
- DecimalToRoman(answerr.Roman,answer);
- cout<<answerr.Roman;
- }
-
- //*************************************************
- //** ROMAN TO DECIMAL FUNCTION **
- //*************************************************
- //** This function converts a roman number to a **
- //** decimal number. It requires the character **
- //** array and the number of characters in the **
- //** array. **
- //*************************************************
- int Romantype::RomanToDecimal(char[MAXLENGTH])
- {
- int number=0; //value of number intialized to zero
- int i; //used in for loop
- for(i=1;i<MAXLENGTH;i++) //until end of array add values of letters
- {
- switch(Roman[i])
- {
- case 'I':number=number+1;break;
- case 'V':number=number+5;break;
- case 'X':number=number+10;break;
- case 'L':number=number+50;break;
- case 'C':number=number+100;break;
- case 'D':number=number+500;break;
- case 'M':number=number+1000;break;
- }
- }
- return number; //return decimal value to where the function was called
- }
-
- //*************************************************
- //** DECIMAL TO ROMAN FUNCTION **
- //*************************************************
- //** This function converts a decimal number to **
- //** a roman number. It requires a character **
- //** array and the value of the decimal number. **
- //*************************************************
- void Romantype::DecimalToRoman(char[MAXLENGTH]/ *??? */ ,int answer)
- ^^^^name of param.?
- ^^^^ this is a char* or a Romantype object
- {
- int i=1; //used in for loop
- while(answer-1000>=0) //convert to roman by storing letters in array
- {
- answerr.Roman[i]='M';
- //^^^^^^Where does the answerr (Romantype object) come from???
- answer=answer-1000;
- i++;
- }
- while(answer-500>=0)
- {
- answerr.Roman[i]='D';
- answer=answer-500;
- i++;
- }
- while(answer-100>=0)
- {
- answerr.Roman[i]='C';
- answer=answer-100;
- i++;
- }
- while(answer-50>=0)
- {
- answerr.Roman[i]='L';
- answer=answer-50;
- i++;
- }
- while(answer-10>=0)
- {
- answerr.Roman[i]='X';
- answer=answer-10;
- i++;
- }
- while(answer-5>=0)
- {
- answerr.Roman[i]='V';
- answer=answer-5;
- i++;
- }
- while(answer-1>=0)
- {
- answerr.Roman[i]='I';
- answer=answer-1;
- i++;
- }
- }
-
-
- //Please review on
- _function_prototype_ and _parameters_passing_
- member function .
-
- //You *miss* some important points
-
- 1. none static member function has a hidden *this* pointer. That is
- this->Roman[i] .... so and so ...
- or just
- Roman[i] .... so and so ... ( better )
-
- 2. Passing a variable in a function call *does not* mean you can use
- that variable in the function definition.
- i.e.
- you call the function and pass 'answerr.Roman' as a parameter
- DecimalToRoman(answerr.Roman,answer);
- and then you want to use 'answerr.Roman' in side the function
-
- BIG MISTAKE
-
- 3. You obmiss the parameter name in the function definition. Is that
- your intense not to use that parameter? If I'm right, you *must* give it a
- name and then use that name in side the function definition instead of
- 'answerr.Roman'
- Please review on function prototype and function definition: they have
- different meaning and syntax
-
-
-
-
-
-
-
-
-
-